﻿//******************************************************************************************************
//  TestAnalyzer.cs - Gbtc
//
//  Copyright © 2010, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
//  not use this file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://www.opensource.org/licenses/eclipse-1.0.php
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  07/30/2010 - Stephen C. Wills
//       Generated original version of source code.
//
//******************************************************************************************************

using System;
using System.Reflection;
using System.Windows.Forms;
using TestBenchApi;

namespace TestAnalyzer
{
    public partial class TestAnalyzer : Form
    {

        #region [ Members ]

        // Fields

        private Analyst m_analyst;

        #endregion

        #region [ Constructors ]

        /// <summary>
        /// Creates a new instance of the <see cref="TestAnalyzer"/> class.
        /// </summary>
        public TestAnalyzer()
        {
            InitializeComponent();
        }

        #endregion

        #region [ Methods ]

        // Occurs when the user chooses to browse for a package file.
        private void packageFileBrowseButton_Click(object sender, EventArgs e)
        {
            if (openPackageFileDialog.ShowDialog() == DialogResult.OK)
                packageFileTextBox.Text = openPackageFileDialog.FileName;
        }

        // Occurs when the user chooses to browse for an assembly.
        private void assemblyNameBrowseButton_Click(object sender, EventArgs e)
        {
            if (assemblyNameOpenFileDialog.ShowDialog() == DialogResult.OK)
                assemblyNameTextBox.Text = assemblyNameOpenFileDialog.FileName;
        }

        // Occurs when the user chooses to start the analysis.
        private void startButton_Click(object sender, EventArgs e)
        {
            if (m_analyst == null || !m_analyst.Running)
            {
                Assembly analystAssembly = Assembly.LoadFile(assemblyNameTextBox.Text);

                m_analyst = analystAssembly.CreateInstance(typeNameTextBox.Text, true) as Analyst;
                m_analyst.AnalystStarted += new EventHandler(m_analyst_AnalystStarted);
                m_analyst.AnalystStopped += m_analyst_AnalystStopped;
                m_analyst.Initialize(packageFileTextBox.Text);
                m_analyst.Start();
            }
            else
            {
                m_analyst.Stop();
            }
        }

        private void SetStartButtonText(string text)
        {
            if (this.InvokeRequired)
                this.Invoke(new Action<string>(SetStartButtonText), text);
            else
                startButton.Text = text;
        }

        private void m_analyst_AnalystStarted(object sender, EventArgs e)
        {
            SetStartButtonText("Stop");
        }

        private void m_analyst_AnalystStopped(object sender, EventArgs e)
        {
            Analyst analyst = sender as Analyst;

            if (analyst != null)
            {
                analyst.AnalystStarted -= m_analyst_AnalystStarted;
                analyst.AnalystStopped -= m_analyst_AnalystStopped;
            }

            if (!m_analyst.Running)
                SetStartButtonText("Start");
        }

        #endregion
    }
}
